<template>
  <el-dialog
    title="短信配置"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item size="mini" label="平台类型">
        <el-radio-group v-model="dataForm.platform">
          <el-radio :label="1">阿里云</el-radio>
          <el-radio :label="2">腾讯云</el-radio>
        </el-radio-group>
      </el-form-item>
      <template v-if="dataForm.platform === 1">
        <el-form-item label="Key" prop="aliyunAccessKeyId">
          <el-input v-model="dataForm.aliyunAccessKeyId" placeholder="阿里云AccessKeyId"></el-input>
        </el-form-item>
        <el-form-item label="Secret" prop="aliyunAccessKeySecret">
          <el-input v-model="dataForm.aliyunAccessKeySecret" placeholder="阿里云AccessKeySecret"></el-input>
        </el-form-item>
        <el-form-item label="短信签名" prop="aliyunSignName">
          <el-input v-model="dataForm.aliyunSignName" placeholder="短信签名"></el-input>
        </el-form-item>
        <el-form-item label="短信模板" prop="aliyunTemplateCode">
          <el-input v-model="dataForm.aliyunTemplateCode" placeholder="短信模板CODE"></el-input>
        </el-form-item>
      </template>
      <template v-else-if="dataForm.platform === 2">
        <el-form-item label="AppId" prop="qcloudAppId">
          <el-input v-model="dataForm.qcloudAppId" placeholder="腾讯云AppId"></el-input>
        </el-form-item>
        <el-form-item label="AppKey" prop="qcloudAppKey">
          <el-input v-model="dataForm.qcloudAppKey" placeholder="腾讯云AppKey"></el-input>
        </el-form-item>
        <el-form-item label="短信签名" prop="qcloudSignName">
          <el-input v-model="dataForm.qcloudSignName" placeholder="短信签名"></el-input>
        </el-form-item>
        <el-form-item label="短信模板" prop="qcloudTemplateId">
          <el-input v-model="dataForm.qcloudTemplateId" placeholder="短信模板ID"></el-input>
        </el-form-item>
      </template>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          platform: 0,
          aliyunAccessKeyId: '',
          aliyunAccessKeySecret: '',
          aliyunSignName: '',
          aliyunTemplateCode: '',
          qcloudAppId: 0,
          qcloudAppKey: '',
          qcloudSignName: '',
          qcloudTemplateId: ''
        },
        dataRule: {
          aliyunAccessKeyId: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          aliyunAccessKeySecret: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          aliyunSignName: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          aliyunTemplateCode: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          qcloudAppId: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          qcloudAppKey: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          qcloudSignName: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          qcloudTemplateId: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    watch: {
      'dataForm.platform' (val) {
        this.$refs['dataForm'].clearValidate()
      }
    },
    methods: {
      init () {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.getConfig()
        })
      },
      // 获取短信配置
      getConfig () {
        this.$http.get(`${window.SITE_CONFIG['baseURL']}/sys/sms/config`).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.platform = data.config.platform
            this.dataForm.aliyunAccessKeyId = data.config.aliyunAccessKeyId
            this.dataForm.aliyunAccessKeySecret = data.config.aliyunAccessKeySecret
            this.dataForm.aliyunSignName = data.config.aliyunSignName
            this.dataForm.aliyunTemplateCode = data.config.aliyunTemplateCode
            this.dataForm.qcloudAppId = data.config.qcloudAppId
            this.dataForm.qcloudAppKey = data.config.qcloudAppKey
            this.dataForm.qcloudSignName = data.config.qcloudSignName
            this.dataForm.qcloudTemplateId = data.config.qcloudTemplateId
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http.post(
              `${window.SITE_CONFIG['baseURL']}/sys/sms/saveConfig`,
              this.dataForm
            ).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
